home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Produtividade / OpenOffice.org 2.0.1 / openofficeorg4.cab / __init__5.py < prev    next >
Text File  |  2005-11-19  |  5KB  |  138 lines

  1. """ Standard "encodings" Package
  2.  
  3.     Standard Python encoding modules are stored in this package
  4.     directory.
  5.  
  6.     Codec modules must have names corresponding to normalized encoding
  7.     names as defined in the normalize_encoding() function below, e.g.
  8.     'utf-8' must be implemented by the module 'utf_8.py'.
  9.  
  10.     Each codec module must export the following interface:
  11.  
  12.     * getregentry() -> (encoder, decoder, stream_reader, stream_writer)
  13.     The getregentry() API must return callable objects which adhere to
  14.     the Python Codec Interface Standard.
  15.  
  16.     In addition, a module may optionally also define the following
  17.     APIs which are then used by the package's codec search function:
  18.  
  19.     * getaliases() -> sequence of encoding name strings to use as aliases
  20.  
  21.     Alias names returned by getaliases() must be normalized encoding
  22.     names as defined by normalize_encoding().
  23.  
  24. Written by Marc-Andre Lemburg (mal@lemburg.com).
  25.  
  26. (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
  27.  
  28. """#"
  29.  
  30. import codecs, exceptions, types
  31.  
  32. _cache = {}
  33. _unknown = '--unknown--'
  34. _import_tail = ['*']
  35. _norm_encoding_map = ('                                              . '
  36.                       '0123456789       ABCDEFGHIJKLMNOPQRSTUVWXYZ     '
  37.                       ' abcdefghijklmnopqrstuvwxyz                     '
  38.                       '                                                '
  39.                       '                                                '
  40.                       '                ')
  41.  
  42. class CodecRegistryError(exceptions.LookupError,
  43.                          exceptions.SystemError):
  44.     pass
  45.  
  46. def normalize_encoding(encoding):
  47.  
  48.     """ Normalize an encoding name.
  49.  
  50.         Normalization works as follows: all non-alphanumeric
  51.         characters except the dot used for Python package names are
  52.         collapsed and replaced with a single underscore, e.g. '  -;#'
  53.         becomes '_'. Leading and trailing underscores are removed.
  54.  
  55.         Note that encoding names should be ASCII only; if they do use
  56.         non-ASCII characters, these must be Latin-1 compatible.
  57.  
  58.     """
  59.     # Make sure we have an 8-bit string, because .translate() works
  60.     # differently for Unicode strings.
  61.     if type(encoding) is types.UnicodeType:
  62.         # Note that .encode('latin-1') does *not* use the codec
  63.         # registry, so this call doesn't recurse. (See unicodeobject.c
  64.         # PyUnicode_AsEncodedString() for details)
  65.         encoding = encoding.encode('latin-1')
  66.     return '_'.join(encoding.translate(_norm_encoding_map).split())
  67.  
  68. def search_function(encoding):
  69.  
  70.     # Cache lookup
  71.     entry = _cache.get(encoding, _unknown)
  72.     if entry is not _unknown:
  73.         return entry
  74.  
  75.     # Import the module:
  76.     #
  77.     # First look in the encodings package, then try to lookup the
  78.     # encoding in the aliases mapping and retry the import using the
  79.     # default import module lookup scheme with the alias name.
  80.     #
  81.     modname = normalize_encoding(encoding)
  82.     try:
  83.         mod = __import__('encodings.' + modname,
  84.                          globals(), locals(), _import_tail)
  85.     except ImportError:
  86.         import aliases
  87.         modname = (aliases.aliases.get(modname) or
  88.                    aliases.aliases.get(modname.replace('.', '_')) or
  89.                    modname)
  90.         try:
  91.             mod = __import__(modname, globals(), locals(), _import_tail)
  92.         except ImportError:
  93.             mod = None
  94.  
  95.     try:
  96.         getregentry = mod.getregentry
  97.     except AttributeError:
  98.         # Not a codec module
  99.         mod = None
  100.  
  101.     if mod is None:
  102.         # Cache misses
  103.         _cache[encoding] = None
  104.         return None
  105.  
  106.     # Now ask the module for the registry entry
  107.     entry = tuple(getregentry())
  108.     if len(entry) != 4:
  109.         raise CodecRegistryError,\
  110.               'module "%s" (%s) failed to register' % \
  111.               (mod.__name__, mod.__file__)
  112.     for obj in entry:
  113.         if not callable(obj):
  114.             raise CodecRegistryError,\
  115.                   'incompatible codecs in module "%s" (%s)' % \
  116.                   (mod.__name__, mod.__file__)
  117.  
  118.     # Cache the codec registry entry
  119.     _cache[encoding] = entry
  120.  
  121.     # Register its aliases (without overwriting previously registered
  122.     # aliases)
  123.     try:
  124.         codecaliases = mod.getaliases()
  125.     except AttributeError:
  126.         pass
  127.     else:
  128.         import aliases
  129.         for alias in codecaliases:
  130.             if not aliases.aliases.has_key(alias):
  131.                 aliases.aliases[alias] = modname
  132.  
  133.     # Return the registry entry
  134.     return entry
  135.  
  136. # Register the search_function in the Python codec registry
  137. codecs.register(search_function)
  138.